﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Configuration;

namespace PromotionEngine
{
    public partial class DocPromotion : System.Web.UI.Page
    {
        public class ResponseGet
        {
            public List<Model.DocPromotionGet> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseGet(List<Model.DocPromotionGet> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseGet(string errors)
            {
                this.Errors = errors;
            }

            public ResponseGet()
            {

            }
        }
        public class ResponseBuy
        {
            public List<Model.DocPromotionBuy> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseBuy(List<Model.DocPromotionBuy> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseBuy(string errors)
            {
                this.Errors = errors;
            }

            public ResponseBuy()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseGet GetDocPromotionGet(string docEntry)
        {
            List<Model.DocPromotionGet> lstGet = new List<Model.DocPromotionGet>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetDocPromotionGetByDocEntry(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), docEntry);
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocPromotionGet docGet = new Model.DocPromotionGet();
                    docGet.ID = r["ID"].ToString();
                    docGet.PromotionID = r["PromotionID"].ToString();
                    docGet.PromotionCode = r["PromotionCode"].ToString();
                    docGet.PromotionName = r["PromotionName"].ToString();
                    docGet.ItemID = r["ItemID"].ToString();
                    docGet.ItemCode = r["ItemCode"].ToString();
                    docGet.ItemName = r["ItemName"].ToString();
                    docGet.LinkKey = r["LinkKey"].ToString();
                    docGet.DocEntry = r["DocEntry"].ToString();
                    docGet.DiscountAmount = double.Parse(r["DiscountAmount"].ToString());
                    docGet.DiscountPercent = double.Parse(r["DiscountPercent"].ToString());
                   // docGet.PriceAfterDiscount = double.Parse(r["PriceAfterDiscount"].ToString());
                    docGet.Quantity = double.Parse(r["Quantity"].ToString());
                    docGet.TotalAfterDiscount = double.Parse(r["TotalAfterDiscount"].ToString());
                    lstGet.Add(docGet);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCompany = lstGet };
                return new ResponseGet(lstGet, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseGet();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseBuy GetDocPromotionBuy(string docEntry)
        {
            List<Model.DocPromotionBuy> lstBuy = new List<Model.DocPromotionBuy>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetDocPromotionBuyByDocEntry(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), docEntry);
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocPromotionBuy docBuy = new Model.DocPromotionBuy();
                    docBuy.ID = r["ID"].ToString();
                    docBuy.PromotionID = r["PromotionID"].ToString();
                    docBuy.PromotionCode = r["PromotionCode"].ToString();
                    docBuy.PromotionName = r["PromotionName"].ToString();
                    docBuy.ItemID = r["ItemID"].ToString();
                    docBuy.ItemCode = r["ItemCode"].ToString();
                    docBuy.ItemName = r["ItemName"].ToString();
                    docBuy.LinkKey = r["LinkKey"].ToString();
                    docBuy.DocEntry = r["DocEntry"].ToString();
                    docBuy.DiscountAmount = double.Parse(r["DiscountAmount"].ToString());
                    docBuy.DiscountPercent = double.Parse(r["DiscountPercent"].ToString());
                    docBuy.PriceAfterDiscount = double.Parse(r["PriceAfterDiscount"].ToString());
                    docBuy.Quantity = double.Parse(r["Quantity"].ToString());
                    docBuy.TotalAfterDiscount = double.Parse(r["TotalAfterDiscount"].ToString());
                    docBuy.BasePrice = double.Parse(r["BasePrice"].ToString());
                    lstBuy.Add(docBuy);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCompany = lstBuy };
                return new ResponseBuy(lstBuy, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseBuy();
        }
    }
}